#ifndef _PTHREADMANAGER_H 
#define _PTHREADMAAGER_H
#include "headfile/allpthread/threadManager.h"
#endif

//#ifndef _QUEUEMANAGER_H
//#define _QUEUEMANAGER_H
//#include"../allqueue/queueManager.h"
//#endif


void initThreadPool(thread_manager * tma)
{
    //实际只有一个,设计的不好
    tma->pt_id = (pthread_t *)malloc(sizeof(pthread_t) * MAX_THREAD_SIZE);
    for(int i = 0 ; i < MAX_LOCK_SIZE ; i++)
        pthread_mutex_init(&(tma->_lock[i]) , NULL);
    for(int i = 0 ; i < MAX_COND_SIZE ; i++)
        pthread_cond_init(&(tma->cond[i]) , NULL);
    tma->idleThreadNum = 0;
    //初始化线程管理器中的任务队列
    initQueueManager(&(tma->qManager));
    printf("threadmanager.c /23: 初始化线程池\n");
}
void startThreadPool(thread_manager * tma)
{
     for(int i = 0 ; i < MAX_THREAD_SIZE ; i++)
     {
         pthread_create(&(tma->pt_id[i]) , NULL , threadWork ,(void *)tma);
         tma->idleThreadNum++;
     }
     printf("threadmanager.c /31: 启动线程池\n");
}

//TODO:可以优化为就绪队列,避免重复唤醒
bool distributeTask(thread_manager * tmanager)
{
    printf("threadmanager.c /38: 分配任务\n");
    //广播唤醒
    pthread_cond_broadcast(&(tmanager->cond[0]));

    printf("threadmanager.c /42:task is %d\n", tmanager->qManager.currSizeTask);










    //qNode * tmp = tmanager->qManager.taskListHead;
    //测试用
    // while(tmp->next != NULL)
    // {
    //     sleep(3);
    //     printf("connectfd is %d , cType is %d , mesType is %d , contentSize is %d\n",
    //            tmp->connect_fd,
    //            tmp->usermes.cType,
    //            tmp->usermes.mesType,
    //            tmp->usermes.contentSize
    //            );
    //     tmp = tmp->next;
    // }
    return true;
}

